package com.copera.cn.kaka;

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.copera.cn.kaka.picpopupwindow.SelectPicPopupWindow;
import com.copera.cn.kaka.util.UploadUtil;
import com.copera.cn.kaka.util.Utility;
import com.copera.cn.kaka.vo.ShopVo;

/**
 * @Description: 我的店铺
 *

 */
public class ShopFragment extends Fragment {
	
	private RelativeLayout rl;//兑换礼品
	private RelativeLayout trackRL;//足迹
	private RelativeLayout photoRL;//拍照
	private TextView ms_text1,ms_text2;
	public static final int SELECT_PIC_BY_PICK_PHOTO = 2;
	private ApplicationCustomer appc;
	private Button ms_btset;
	// 自定义的弹出框类
    private SelectPicPopupWindow menuWindow;
	
	public static ShopFragment newInstance(int index) {
		ShopFragment f = new ShopFragment();

		
		Bundle args = new Bundle();
		args.putInt("index", index);
		f.setArguments(args);

		return f;
	}

	public int getShownIndex() {
		return getArguments().getInt("index", 0);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.activity_myshop, container, false);
		appc = (ApplicationCustomer) getActivity().getApplication();
		initViews(view);
		initValues();
	    initEvent();
		return view;
	}
	
	private void initViews(View view){
		rl = (RelativeLayout)view.findViewById(R.id.ms_rl27);
	    trackRL = (RelativeLayout)view.findViewById(R.id.ms_rl26);
	    photoRL = (RelativeLayout)view.findViewById(R.id.ms_rl2);
	    ms_btset = (Button)view.findViewById(R.id.ms_btset);
	    ms_text2 = (TextView)view.findViewById(R.id.ms_text2);
	    ms_text1 = (TextView)view.findViewById(R.id.ms_text1);
	}
	private void initValues(){
		ShopVo shop = appc.getShop();
		ms_text1.setText(shop.getShopName());
		ms_text2.setText(shop.getAddress());
	}
	private void initEvent(){
		rl.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				Intent intent = new Intent(getActivity(),MyShopTableHostActivity.class);
			    startActivity(intent);
			}
		});
        trackRL.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				Intent intent = new Intent(getActivity(),MyShopTrackTableHostActivity.class);
			    startActivity(intent);
			}
		});
        ms_btset.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				Intent intent = new Intent(getActivity(),ShopSetHomeActivity.class);
			    startActivity(intent);
			}
		});
        photoRL.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// 实例化SelectPicPopupWindow
				menuWindow = new SelectPicPopupWindow(getActivity(),itemsOnClick);
				// 显示窗口
				menuWindow.showAtLocation(
						getActivity().findViewById(R.id.main),
						Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0); // 设置layout在PopupWindow中显示的位置 

			}
		});
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		

	}
	private Uri photoUri;
	private String picPath;
	private void doPhoto(int requestCode, Intent data) {

		if (requestCode == SELECT_PIC_BY_PICK_PHOTO) // 从相册取图片，有些手机有异常情况，请注意
		{
			if (data == null) {
				Toast.makeText(getActivity(), "选择图片文件出错", Toast.LENGTH_LONG).show();
				return;
			}
			photoUri = data.getData();
			if (photoUri == null) {
				Toast.makeText(getActivity(), "选择图片文件出错", Toast.LENGTH_LONG).show();
				return;
			}
		}
		String[] pojo = { MediaStore.Images.Media.DATA };

		//Cursor cursor = managedQuery(photoUri, pojo, null, null, null);
		ContentResolver cr = getActivity().getContentResolver();
		Cursor cursor=cr.query(photoUri, pojo, null, null, null);
		if (cursor != null) {
			int columnIndex = cursor.getColumnIndexOrThrow(pojo[0]);
			cursor.moveToFirst();
			picPath = cursor.getString(columnIndex);
			cursor.close();
		}

		if (picPath != null
				&& (picPath.endsWith(".png") || picPath.endsWith(".PNG")
						|| picPath.endsWith(".jpg") || picPath.endsWith(".JPG"))) {
			String[] paths = null;
		
			Bitmap bp = Utility.getimage(picPath);
			File file = null;
			try {
				 file = UploadUtil
						.saveMyBitmap(bp);
				 bp = Utility.getimage(file.getAbsolutePath());
				 BitmapDrawable bd = new BitmapDrawable(Utility.small(bp));
				 photoRL.setBackgroundDrawable(bd);
				
			} catch (IOException e) {
				
				e.printStackTrace();
			}
			

		} else {
			Toast.makeText(getActivity(), "选择图片文件不正确", Toast.LENGTH_LONG).show();
		}
	}

	@SuppressWarnings("deprecation")
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		 if (resultCode == 10) { 
			 
				BitmapDrawable bd = new BitmapDrawable(Utility.small(appc.getBmImage()));
			 photoRL.setBackgroundDrawable(bd);
		 }else if (resultCode == Activity.RESULT_OK) {
				doPhoto(requestCode, data);
			}
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	public void onHiddenChanged(boolean hidden) {
		super.onHiddenChanged(hidden);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}
	// 为弹出窗口实现监听类
	private OnClickListener itemsOnClick = new OnClickListener() {

		public void onClick(View v) {
			menuWindow.dismiss();
			Intent intent = null;
			int id = v.getId();
			if (id == R.id.btn_take_photo) {
				
				intent = new Intent(getActivity(),PhotoActivity.class);
				startActivityForResult(intent, 100);
				getActivity().overridePendingTransition(R.layout.push_left_in, R.layout.push_left_out);
			} else if (id == R.id.btn_pick_photo) {
				intent = new Intent();
				intent.setType("image/*");
				intent.setAction(Intent.ACTION_GET_CONTENT);
				startActivityForResult(intent, SELECT_PIC_BY_PICK_PHOTO);
				appc.setSelectType("zm");
			} else {
				//finish();
			}

		}

	};	
	
	
	
}
